package com.studentapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.studentapp.R;
import com.studentapp.bean.MyComment;
import com.studentapp.bean.OthersComment;

public class CommentAdapter extends BaseAdapter {
    private static final String TAG = "CommentAdapter";
    private List<Object> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    private static final int TYPE_OTHERS = 0;
    private static final int TYPE_ME = 1;

    public CommentAdapter(Context context,List<Object> objects) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //多布局的核心，通过这个判断类别
    @Override
    public int getItemViewType(int position) {
        if (objects.get(position) instanceof OthersComment) {
            return TYPE_OTHERS;
        } else if (objects.get(position) instanceof MyComment) {
            return TYPE_ME;
        } else {
            return super.getItemViewType(position);
        }
    }

    //类别的数目
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        Log.d(TAG, "getView: "+type);
        if (convertView == null) {
            switch (type) {
                case TYPE_OTHERS:
                    convertView = layoutInflater.inflate(R.layout.item_list_comment_others, null);
                    break;
                case TYPE_ME:
                    convertView = layoutInflater.inflate(R.layout.item_list_comment_my, null);
                    break;
            }
        }
        switch (type) {
            case TYPE_OTHERS:
                convertView.setTag(new OthersViewHolder(convertView));
                initializeViewsOthers((OthersComment) getItem(position), (OthersViewHolder) convertView.getTag());
                break;
            case TYPE_ME:
                convertView.setTag(new MyViewHolder(convertView));
                initializeViewsMy((MyComment) getItem(position), (MyViewHolder) convertView.getTag());
                break;
        }
        return convertView;
    }

    private void initializeViewsOthers(OthersComment object, OthersViewHolder holder) {
        holder.ivUserImg.setImageResource(R.mipmap.ic_launcher);
        holder.tvComment.setText(object.getComment());
    }

    private void initializeViewsMy(MyComment object, MyViewHolder holder) {
        holder.ivUserImg.setImageResource(R.mipmap.ic_launcher);
        holder.tvComment.setText(object.getComment());
    }

    protected class OthersViewHolder {
        private ImageView ivUserImg;
        private TextView tvComment;

        public OthersViewHolder(View view) {
            ivUserImg = (ImageView) view.findViewById(R.id.iv_userImg_other);
            tvComment = (TextView) view.findViewById(R.id.tv_comment_other);
        }
    }

    protected class MyViewHolder {
        private ImageView ivUserImg;
        private TextView tvComment;

        public MyViewHolder(View view) {
            ivUserImg = (ImageView) view.findViewById(R.id.iv_userImg_me);
            tvComment = (TextView) view.findViewById(R.id.tv_comment_me);
        }
    }
}
